import com.fasterxml.jackson.databind.ObjectMapper;
import sun.util.resources.cldr.ar.CalendarData_ar_YE;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-24
 * Time: 22:25
 */
class Message {
    public String from;
    public String to;
    public String message;
}

@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    //private List<Message> list = new ArrayList<>();
    private ObjectMapper objectMapper = new ObjectMapper();

    //服务器提交数据到这里
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Message message = objectMapper.readValue(req.getInputStream(), Message.class);
        //list.add(message);
        //将数据保存在数据库中
        try {
            save(message);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.setStatus(200);
    }

    //服务器从这里获取数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        //objectMapper.writeValue(resp.getWriter(), list);
        List<Message> list = load();
        String message = objectMapper.writeValueAsString(list);
        resp.getWriter().write(message);
    }

    public void save(Message message) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //1、建立连接
            connection = DataUtil.getConnection();
            //2、构建sql语句
            String sql = "insert into wall values (?,?,?)";//表示占位符
            statement = connection.prepareStatement(sql);
            statement.setString(1, message.from);
            statement.setString(2, message.to);
            statement.setString(3, message.message);
            //3、执行sql语句
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4、关闭连接
            DataUtil.close(connection, statement, null);
        }
    }

    public List<Message> load() {
        List<Message> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DataUtil.getConnection();
            //构建sql
            String sql = "select * from wall";
            statement = connection.prepareStatement(sql);
            //执行sql
            resultSet = statement.executeQuery();
            //遍历结果集合
            while (resultSet.next()) {
                Message message = new Message();
                message.from = resultSet.getString("from");
                message.to = resultSet.getString("to");
                message.message = resultSet.getString("message");
                list.add(message);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DataUtil.close(connection, statement, resultSet);
        }
        return list;
    }
}
